package com.yexs.common.util;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.yexs.common.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @Description: Json
 * @Author: yexs
 * @CreateDate: 2018/5/15 8:54
 * @Version: 1.0
 */
public final class JsonUtils {

    private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    private static ObjectMapper mapper = new ObjectMapper();

    private JsonUtils() {
    }

    public static String toJacksonJson(Object value) {
        try {
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            mapper.setSerializationInclusion(Include.NON_NULL);
            return mapper.writeValueAsString(value);
        } catch (Exception e) {
            logger.error("JSON解析失败", e);
        }
        return null;
    }

    public static String toJson(Object value) {
        return JSON.toJSONString(value);
    }

    public static <T> List<T> toArray(String json, Class<T> valueType) {
        return JSON.parseArray(json, valueType);
    }

    public static Map<String, Object> toObject(final String json) {
        return toObject(json, new TypeReference<Map<String, Object>>() {
        });
    }

    public static <T> T toObject(String json, Class<T> valueType) {
        if (json == null || json.isEmpty()) {
            return null;
        }
        return toObjectWithResult(json, valueType).getResult();
    }

    public static <T> T toObject(String json, TypeReference<?> typeReference) {
        if (json == null || json.isEmpty()) {
            return null;
        }
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception e) {
            logger.error("JSON解析失败", e);
        }
        return null;
    }

    private static <T> Result<T> toObjectWithResult(String json, Class<T> valueType) {
        if (json == null || json.isEmpty()) {
            return null;
        }
        try {
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return Result.SUCCESS(mapper.readValue(json, valueType));
        } catch (Exception e) {
            logger.error("JSON解析失败", e);
            return Result.ERROR(e.getMessage());
        }
    }

    /**
     * 将对象转换为JSON字符串  那些对象的哪些字段需要显示
     */
    public static String toJsonWithSerializePropertys(Object object, Map<String, String[]> objectAndPropertys) {
        if (objectAndPropertys != null && !objectAndPropertys.isEmpty()) {
            Iterator<Entry<String, String[]>> iterator = objectAndPropertys.entrySet().iterator();
            SimpleFilterProvider filterProvider = new SimpleFilterProvider();
            while (iterator.hasNext()) {
                String objectName = iterator.next().getKey();
                filterProvider.addFilter(objectName, SimpleBeanPropertyFilter.filterOutAllExcept(objectAndPropertys.get(objectName)));
            }
            mapper.setFilters(filterProvider);
        }
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            logger.error("JSON解析失败", e);
            return "[]";
        }
    }

}